package com.smile.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.smile.api.OperateLogApi;
import com.smile.api.OrganizationApi;
import com.smile.api.OssApi;
import com.smile.commons.action.ActionUtils;
import com.smile.commons.gps.GeoConvertUtils;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.commons.time.TimeUtils;
import com.smile.mapper.*;
import com.smile.model.domain.*;
import com.smile.model.dto.DroneDTO;
import com.smile.model.dto.request.*;
import com.smile.model.response.response.*;
import com.smile.model.request.GetOrganizationByIdRequest;
import com.smile.model.response.GetOrganizationByIdResponse;
import com.smile.request.AddOperateLogRequest;
import com.smile.service.AirportService;
import com.smile.service.FlightTaskProcessService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

@Service
@Slf4j
public class FlightTaskProcessServiceImpl implements FlightTaskProcessService {

    @Autowired
    private OssApi ossApi;

    @Autowired
    private AirportMapper airportMapper;

    @Autowired
    private FlightTaskMapper flightTaskMapper;

    @Autowired
    private InspectionTaskMapper inspectionTaskMapper;
    @Autowired
    private AirportDroneMapper airportDroneMapper;

    @Autowired
    private DroneMapper droneMapper;

    @Autowired
    private AirLineMapper airLineMapper;

    private ReentrantLock reentrantLock = new ReentrantLock();


    @Autowired
    private AirportInfoMapper airportInfoMapper;

    @Override
    public Page<GetFlightTaskResponseDTO> getFlightTaskList(GetFlightTaskListRequestDTO getAirportListRequestDTO) {

        QueryWrapper<FeisuanAirportDrone> queryWrapper1 = new QueryWrapper<>();
        System.out.println(getAirportListRequestDTO.getDroneId());
        queryWrapper1.eq("drone_id",getAirportListRequestDTO.getDroneId());
        queryWrapper1.last("limit 1");
        FeisuanAirportDrone feisuanAirportDrone =  airportDroneMapper.selectOne(queryWrapper1);
        if (ObjectUtil.isNotNull(feisuanAirportDrone)) {
            QueryWrapper<FeisuanFlightTask> queryWrapper = new QueryWrapper<>();
            FeisuanAirport feisuanAirport = airportMapper.selectById(feisuanAirportDrone.getAirportId());
            queryWrapper.eq("gateway_sn", feisuanAirport.getGatewaySn());
            queryWrapper.orderByDesc("id");
            System.out.println(feisuanAirport.getGatewaySn());
            Page<FeisuanFlightTask> page = flightTaskMapper.selectPage(new Page<>(getAirportListRequestDTO.getCurrentPage(), getAirportListRequestDTO.getPageSize()), queryWrapper);
            Page<GetFlightTaskResponseDTO> getAirportListResponseDTOPage = MyBatisPlusPageConverter.convertPage(page, GetFlightTaskResponseDTO.class);

            getAirportListResponseDTOPage.setRecords(page.getRecords().stream().map(i -> {
//                /**
//                 * 计划名称
//                 */
//                private String planName;
//
//                /**
//                 *机场或飞手名称
//                 */
//                private String airportOrPilot;
//
//                /**
//                 * 执行航线
//                 */
//                private String airline;
                GetFlightTaskResponseDTO getFlightTaskResponseDTO = BeanUtil.copyProperties(i, GetFlightTaskResponseDTO.class);
                getFlightTaskResponseDTO.setAirportOrPilot(feisuanAirport.getAirportName());
                if(getFlightTaskResponseDTO.getTaskType() == 1){ //航线任务
                    QueryWrapper<FeisuanInspectionTask> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("flight_id",getFlightTaskResponseDTO.getFlightId());
                    queryWrapper2.last("limit 1");
                    FeisuanInspectionTask inspectionTask = inspectionTaskMapper.selectOne(queryWrapper2);
                    if(ObjectUtil.isNotNull(inspectionTask)){
                        getFlightTaskResponseDTO.setPlanName(inspectionTask.getPlanName());
                        FeisuanAirline feisuanAirline = airLineMapper.selectById(inspectionTask.getAirlineId());
                        if(ObjectUtil.isNotNull(feisuanAirline)){
                            System.out.println(inspectionTask.getAirlineId());
                            System.out.println(feisuanAirline.getAirlineName());
                            getFlightTaskResponseDTO.setAirline(feisuanAirline.getAirlineName());
                        }
                    }
                }else if(getFlightTaskResponseDTO.getTaskType() == 2){
                    getFlightTaskResponseDTO.setPlanName("一键起飞");
                }
                return getFlightTaskResponseDTO;
            }).collect(Collectors.toList()));

            return getAirportListResponseDTOPage;
        }else{
            return null;
        }
    }
}
